package com.wzh.easyapi.controller;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzh.easyapi.base.R;
import com.wzh.easyapi.bean.DataSourceEntity;
import com.wzh.easyapi.service.DataSourceService;
import com.wzh.easyapi.service.DynamicSQLService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.util.List;

/**
 * 数据源管理
 */
@RestController
@RequestMapping("/datasource")
public class DataSourceController {

    @Autowired
    private DataSourceService dataSourceService;

    @Autowired
    private DynamicSQLService dynamicSQLService;

    /**
     * 新增数据源
     * @param entity
     * @return
     */
    @PostMapping
    public R<Boolean> createDataSource(@RequestBody DataSourceEntity entity) {
        DataSourceEntity one = dataSourceService.getOne(new LambdaQueryWrapper<DataSourceEntity>()
                .eq(DataSourceEntity::getName, entity.getName()));
        if (one != null) {
           return R.fail("数据源名称:"+entity.getName()+"已存在");
        }
        return R.ok(dataSourceService.save(entity));
    }

    /**
     * 数据源测试连接
     * @param entity
     * @return
     */
    @PostMapping("/testConnect")
    public R<Boolean> testConnect(@RequestBody DataSourceEntity entity) {
        DataSource dataSource = dynamicSQLService.getDataSource(entity);
        try {
            dynamicSQLService.testConnection(dataSource);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok(true);
    }

    /**
     * 更新数据源
     * @param id
     * @param entity
     * @return
     */
    @PutMapping("/{id}")
    public R<Boolean> updateDataSource(@PathVariable String id, @RequestBody DataSourceEntity entity) {
        entity.setId(id);
        DataSourceEntity one = dataSourceService.getById(id);
        if (!one.getName().equals(entity.getName())) {
            DataSourceEntity nameEntity = dataSourceService.getOne(new LambdaQueryWrapper<DataSourceEntity>()
                    .eq(DataSourceEntity::getName, entity.getName()));
            if (nameEntity != null) {
               return R.fail("数据源名称:"+entity.getName()+"已存在");
            }
        }
        return R.ok(dataSourceService.updateById(entity));
    }

    /**
     * 删除数据源
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R<Boolean> deleteDataSource(@PathVariable String id) {
        return R.ok(dataSourceService.removeById(id));
    }


    /**
     * 获取单个数据源
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DataSourceEntity> getDataSource(@PathVariable String id) {
        return R.ok(dataSourceService.getById(id));
    }


    /**
     * 获取所有数据源
     * @return
     */
    @GetMapping
    public R<List<DataSourceEntity>> getAllDataSources() {
        return R.ok(dataSourceService.list());
    }


    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DataSourceEntity>> getDataSourcesByPage(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) String name) {
        return R.ok(dataSourceService.page(new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<DataSourceEntity>()
                        .like(ObjUtil.isNotEmpty(name),DataSourceEntity::getName, name)
                        .orderByDesc(DataSourceEntity::getCreateTime)));
    }



}
